package com.example.mylazyviewpager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private final String TAG = "MainActivity";
    private ViewPager mViewPager;
    private BottomNavigationView bottomNavigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate: ");
        setContentView(R.layout.activity_main);

//        simpleFragment();
//        lazyFragment_1_error();

//        lazyFragment_1_bugfix();
//        lazyFragment_2();
//        lazyFragment_3();

        lazyFragment_4();
    }

    private void lazyFragment_4(){
        //TODO 第4版 解决Fragment与Activity互相跳转不执行加载与停止加载
        mViewPager = findViewById(R.id.viewPager);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);

        ArrayList<Fragment>  fragmentList = new ArrayList<>();
        fragmentList.add(new MyFragment4(1));
        fragmentList.add(new MyFragment_to_Activity(2));
        fragmentList.add(new MyFragment4(3));
        fragmentList.add(new MyFragment4(4));
        fragmentList.add(new MyFragment4(5));
        MyFragmentAdaptor adaptor = new MyFragmentAdaptor(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(adaptor);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setOnPageChangeListener(viewpagerChangeListener);
    }
    private void lazyFragment_3(){
        //TODO 第3版 解决第2版 有些fragment只可见了一次，但是在多次跳转中，执行了多次停止加载
        mViewPager = findViewById(R.id.viewPager);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);

        ArrayList<MyFragment4>  fragmentList = new ArrayList<>();
        fragmentList.add(new MyFragment4(1));
        fragmentList.add(new MyFragment4(2));
        fragmentList.add(new MyFragment4(3));
        fragmentList.add(new MyFragment4(4));
        fragmentList.add(new MyFragment4(5));
        MyFragmentAdaptor adaptor = new MyFragmentAdaptor(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(adaptor);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setOnPageChangeListener(viewpagerChangeListener);

    }

    private void lazyFragment_2(){
        //TODO 第2版 解决第1版 Fragment一直处于加载中的bug
        //但是仍然还有一个bug，虽然有些fragment不可见，仍然会加载
        mViewPager = findViewById(R.id.viewPager);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);

        ArrayList<MyFragment3>  fragmentList = new ArrayList<>();
        fragmentList.add(new MyFragment3(1));
        fragmentList.add(new MyFragment3(2));
        fragmentList.add(new MyFragment3(3));
        fragmentList.add(new MyFragment3(4));
        fragmentList.add(new MyFragment3(5));
        MyFragmentAdaptor adaptor = new MyFragmentAdaptor(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(adaptor);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setOnPageChangeListener(viewpagerChangeListener);

    }

    private void lazyFragment_1_bugfix(){
        //TODO 第1版 ：解决预加载失败， 但是还有第二个Bug:Fragment内部控件始终处于初始化状态
        //原因是 我们把真正的加载操作放在了setUserVisibleHint里面，而init并未调用setUserVisibleHint
        mViewPager = findViewById(R.id.viewPager);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);

        ArrayList<MyFragment2>  fragmentList = new ArrayList<>();
        fragmentList.add(new MyFragment2(1));
        fragmentList.add(new MyFragment2(2));
        fragmentList.add(new MyFragment2(3));
        fragmentList.add(new MyFragment2(4));
        fragmentList.add(new MyFragment2(5));
        MyFragmentAdaptor adaptor = new MyFragmentAdaptor(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(adaptor);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setOnPageChangeListener(viewpagerChangeListener);
    }

    private void lazyFragment_1_error(){
        //TODO 第1版 ：演示预加载失败
        //原因是 先走Fragment的 setUserVisibleHint()，然后才调用 生命周期函数onCreateView()， onCreateView内才完成Fragment内部控件初始化的逻辑;也就是说第一次走setUserVisibleHint()控件未初始化，但是调用了控件的方法，报了空指针的错误
        mViewPager = findViewById(R.id.viewPager);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);

        ArrayList<MyFragment1>  fragmentList = new ArrayList<>();
        fragmentList.add(new MyFragment1(1));
        fragmentList.add(new MyFragment1(2));
        fragmentList.add(new MyFragment1(3));
        fragmentList.add(new MyFragment1(4));
        fragmentList.add(new MyFragment1(5));
        MyFragmentAdaptor adaptor = new MyFragmentAdaptor(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(adaptor);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setOnPageChangeListener(viewpagerChangeListener);
    }

    private void simpleFragment(){
        //TODO 第0版 ：演示 预加载
        mViewPager = findViewById(R.id.viewPager);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);

        ArrayList<SimpleFragment>  fragmentList = new ArrayList<>();
        fragmentList.add(new SimpleFragment(1));
        fragmentList.add(new SimpleFragment(2));
        fragmentList.add(new SimpleFragment(3));
        fragmentList.add(new SimpleFragment(4));
        fragmentList.add(new SimpleFragment(5));
        SimpleFragmentAdaptor adaptor = new SimpleFragmentAdaptor(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(adaptor);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setOnPageChangeListener(viewpagerChangeListener);
    }

    ViewPager.OnPageChangeListener viewpagerChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            int itemId = R.id.fragment_1;
            switch (position){
                case 0:
                    itemId = R.id.fragment_1;
                    break;
                case 1:
                    itemId = R.id.fragment_2;
                    break;
                case 2:
                    itemId = R.id.fragment_3;
                    break;
                case 3:
                    itemId = R.id.fragment_4;
                    break;
                case 4:
                    itemId = R.id.fragment_5;
                    break;
            }
            bottomNavigationView.setSelectedItemId(itemId);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            Log.e(TAG, "onNavigationItemSelected: "+menuItem.getItemId());
            switch (menuItem.getItemId()){
                case R.id.fragment_1:
                    mViewPager.setCurrentItem(0, true);
                    return true;
                case R.id.fragment_2:
                    mViewPager.setCurrentItem(1, true);
                    return true;
                case R.id.fragment_3:
                    mViewPager.setCurrentItem(2, true);
                    return true;
                case R.id.fragment_4:
                    mViewPager.setCurrentItem(3, true);
                    return true;
                case R.id.fragment_5:
                    mViewPager.setCurrentItem(4, true);
                    return true;
            }
            return false;
        }
    };
}